package com.yanqu.road.dao.impl.kowloon;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.kowloon.KowloonBuildingData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class KowloonBuildingDataDaoImpl extends TempDataObjectDao<KowloonBuildingData> {

    public static String getKowloonBuildingDataSql = "select * from `t_u_kowloon_building_data` where `userId`=?";

    public Map<Integer, KowloonBuildingData> getKowloonBuildingDataMap(long userId) {
        Map<Integer, KowloonBuildingData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getKowloonBuildingDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                KowloonBuildingData buildingData = parse(rs);
                map.put(buildingData.getBuildingId(), buildingData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateKowloonBuildingDataSql = "update `t_u_kowloon_building_data` set level=?,updateTime=? where userId=? and buildingId=?";
    @Override
    protected boolean updateData(KowloonBuildingData kowloonBuildingData) {
        kowloonBuildingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, kowloonBuildingData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonBuildingData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonBuildingData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonBuildingData.getBuildingId()));
        if (getDb().executeNoneQuery(updateKowloonBuildingDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertKowloonBuildingDataSql = "insert into `t_u_kowloon_building_data`(userId,buildingId,level,updateTime) values (?,?,?,?)";
    @Override
    protected boolean addData(KowloonBuildingData kowloonBuildingData) {
        kowloonBuildingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, kowloonBuildingData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonBuildingData.getBuildingId()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonBuildingData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonBuildingData.getUpdateTime()));
        if (getDb().executeNoneQuery(insertKowloonBuildingDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected KowloonBuildingData parseData(ResultSet rs) throws SQLException {
        KowloonBuildingData kowloonBuildingData = new KowloonBuildingData();
        kowloonBuildingData.setUserId(rs.getLong("userId"));
        kowloonBuildingData.setBuildingId(rs.getInt("buildingId"));
        kowloonBuildingData.setLevel(rs.getInt("level"));
        kowloonBuildingData.setUpdateTime(rs.getLong("updateTime"));

        return kowloonBuildingData;
    }

}
